package com.polaris.live.ui.main.fragment

import androidx.recyclerview.widget.GridLayoutManager
import com.polaris.live.R
import com.polaris.live.adapter.home.HomeManAdapter
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.FragmentHotManBinding
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.interfaces.OnAutoRefreshListener
import com.polaris.live.utils.interfaces.OnRefreshStateListener
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.home.HotViewModel

/**
 * 热门主页-男
 *
 * @author Created by lucas on 2023/10/16 13:32
 * @since 1.0.0
 */
class HotManFragment : AbstractHotManFragment<HotViewModel, FragmentHotManBinding>(),
    OnAutoRefreshListener, OnRefreshStateListener {

    override val refreshView by lazy { mBinding.refresh }

    override val layoutManager by lazy {
        GridLayoutManager(
            context,
            2,
            GridLayoutManager.VERTICAL,
            false
        )
    }

    override val mAdapter by lazy { HomeManAdapter() }

    override val listLiveData by lazy { mViewModel.listLiveData }

    override val promptBannerView by lazy { mBinding.bvPrompt }

    override fun initListener() {
        mBinding.call.apply {
            setViewModel(mViewModel)
            setPage(UserConst.From.HOT)
            setFragment(this@HotManFragment)
            setActivity(requireActivity())
            setLastPage(UserConst.LastFrom.HOT)
        }
        mBinding.rechargeAc.registerObserve(lifecycle, mViewModel)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mAdapter.getItem(position)
            if (item is DisplayUserBean) {
                when (view.id) {
                    R.id.user_cover -> {
                        GlobalPopupManager.showCompletePopup(requireContext()) {
                            Router.builder(RoutePath.USER_SPACE_ACTIVITY)
                                .withInt("page", UserConst.From.HOT)
                                .withLong("userId", item.base.userId)
                                .withAny("userBasic", item.base)
                                .navigation(requireActivity())
                        }
                    }

                    R.id.bg_call -> {
                        StartCallProxy.builder(this).startCall(
                            mViewModel,
                            UserConst.From.HOT,
                            UserConst.LastFrom.HOT,
                            LiveConst.SenderFrom.MANUAL,
                            item.base.userId,
                            item.base
                        )
                    }
                }
            }
        }
    }

    override fun onRefreshState() {
        if (!isCreated()) return
        mBinding.call.anewView()
    }
}